package cn.iocoder.yudao.module.moa.controller.admin.ccapply;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.moa.controller.admin.ccapply.vo.*;
import cn.iocoder.yudao.module.moa.dal.dataobject.ccapply.CcApplyDO;
import cn.iocoder.yudao.module.moa.service.ccapply.CcApplyService;

/**
 * 出差申请
 */
@Tag(name = "管理后台 - 出差申请")
@RestController
@RequestMapping("/moa/cc-apply")
@Validated
public class CcApplyController {

    @Resource
    private CcApplyService ccApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建出差申请")
    @PreAuthorize("@ss.hasPermission('moa:cc-apply:create')")
    public CommonResult<Long> createCcApply(@Valid @RequestBody CcApplySaveReqVO createReqVO) {
        return success(ccApplyService.createCcApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新出差申请")
    @PreAuthorize("@ss.hasPermission('moa:cc-apply:update')")
    public CommonResult<Boolean> updateCcApply(@Valid @RequestBody CcApplySaveReqVO updateReqVO) {
        ccApplyService.updateCcApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除出差申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('moa:cc-apply:delete')")
    public CommonResult<Boolean> deleteCcApply(@RequestParam("id") Long id) {
        ccApplyService.deleteCcApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得出差申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('moa:cc-apply:query')")
    public CommonResult<CcApplyRespVO> getCcApply(@RequestParam("id") Long id) {
        CcApplyDO ccApply = ccApplyService.getCcApply(id);
        return success(BeanUtils.toBean(ccApply, CcApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得出差申请分页")
    @PreAuthorize("@ss.hasPermission('moa:cc-apply:query')")
    public CommonResult<PageResult<CcApplyRespVO>> getCcApplyPage(@Valid CcApplyPageReqVO pageReqVO) {
        PageResult<CcApplyDO> pageResult = ccApplyService.getCcApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CcApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出出差申请 Excel")
    @PreAuthorize("@ss.hasPermission('moa:cc-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCcApplyExcel(@Valid CcApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CcApplyDO> list = ccApplyService.getCcApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "出差申请.xls", "数据", CcApplyRespVO.class,
                        BeanUtils.toBean(list, CcApplyRespVO.class));
    }

}
